<?php
//首页默认的模块
class IndexControl extends Control{
	// 首页默认显示的页面
	public function index(){
		// p(category());
		//调取栏目的数据
		$category=M("category")->all();
		$category=Data::channelLevel($category);
		//分配热门搜索
		$this->assign("hot",hot());
		//分配购物车里面的商品
		$this->assign("cart",cart());
		$this->assign("category",$category);
		$this->display("index");
	}
	// 栏目页(列表页)
	public function category(){
		// 获得栏目缓存
		$category=cache("category");
		//获得当前请求的栏目id
		$cid=Q("cid");
		//获得商品的类型id
		$gtid=$category[$cid]['gtid'];
		// p($gtid);
		//获得该商品下面商品属性的数量
		$attrNum=M("g_attr_class")->where(array("add_goods_type_aid"=>$gtid))->count();
		// p($attrNum);
		// 判断url是从哪里来,
		if(!Q("get.s")){//如果是从首页来的话，我们为他配一个商品属性的地址
			$s='';
			for($i=0;$i<$attrNum;$i++){
				$s.="0-";
			}
			$s=substr($s,0,-1);
			go(U("category",array("cid"=>$cid,"s"=>$s)));
		}
		// 分配热卖产品
		$this->like($cid);
		//向栏目页分配商品搜索属性
		$this->assignSearchAttr($gtid);	
		//想栏目页分配商品内容
		$this->assignGoods($cid); 
		// 向栏目页分配栏目名称
		$cname=$category[$cid]['cname'];
		$this->assign('cname',$cname);
		// 分配所有的栏目
		$this->assign("categoryInfo",category());
		// 分配热门搜索
		$this->assign("hot",hot());
		//分配购物车里面的商品
		$this->assign("cart",cart());
		// 分配左边二级栏目以及下面的所有的三级菜单
		$this->precategory($cid);
		$this->display("goods_list");
	}
	//分配左边二级栏目以及下面所有的三级菜单
	private function precategory($cid){
		$category=cache('category');
		$currentCategory=$category[$cid];
		$parentCategory=$category[$currentCategory['pid']];
		$brotherCategory=array();
		foreach( $category as $cid=>$cate){//查找出当前栏目的父级栏目的所有兄弟及的栏目
			if($cate['pid']==$parentCategory['pid']){
				$brotherCategory[$cid]=$cate;
			}
			continue;
		}
		foreach($brotherCategory as $n=>$borther){//查找兄弟及的所有的子栏目，并且把他们压到子栏目当中
			foreach($category as $k=>$cate){
				if($cate['pid']==$borther['cid']){
					$brotherCategory[$n]['childCategory'][]=$cate;
				}
			}
		}
		$this->assign("brotherCategory",$brotherCategory);
	}


	// 列表页热卖产品
	private function like($cid){
		$like_goods=M("goods")->where(array('cid'=>$cid))->order('price asc')->limit(3)->all();
		$this->assign("like_goods",$like_goods);
	}

	// 内容页，对内容页的显示
	public function content(){
		$gid=Q("gid");
		// 栏目缓存
		$category=cache("category");
		// 商品类型缓存
		$goodsType=cache("goodsType");
		$goods=M("goods")->where(array("gid"=>$gid))->find();
		//商品内容的图片
		$pic=M("goods_pic")->where(array("goods_gid"=>$gid))->find();
		// 把商品类型给拿出来了
		$gtid=$category[$goods['cid']]['gtid'];
		// 查该商品类型下面有哪些属性
		$attrClass=M("g_attr_class")->where(array("add_goods_type_aid"=>$gtid,"is_spec"=>1))->all();
		// 把每个属性下面的值压倒属性当中去
		if(!is_null($attrClass)){
			foreach($attrClass as $n=>$attr){
			$attrValue=M("g_arr_value")->where(array("g_attr_class_ac_id"=>$attr['ac_id']))->all();
			$attrClass[$n]['attr']=$attrValue;
			}
		}
		// 获得商品该商品的属性
		// 组合sql筛选掉没有选定的属性值
		$sql="select * from g_arr_value as av join goods_attr as ga on av.av_id=ga.avid where gid=".$gid; 
		$goods_attr=M()->query($sql);
		// 让规格显示
		if(!Q("spec")){//没有spec说明是从列表页过来的，反之说明用户点击了规格
			$stock=M("stock")->where(array("goods_gid"=>$gid))->find();
			$stockData=M('stock_attr')->where(array("stock_st_id"=>$stock["st_id"]))->getField("attr_value_av_id",true);
		}else{
			$attr=Q("spec");
			$gid=Q("gid");
			$attr=explode("-",$attr);
			$stock=$this->getStockId($gid,$attr);
			// 指定的库存就得到了
			$stockData=M("stock_attr")->where(array("stock_st_id"=>$stock[0]['st_id']))->getField("attr_value_av_id",true);
			$stock=$stock[0];
		}
		$goods=array_merge($goods,$stock);
		//查找这个商品属于哪个栏目下
		$category=M('category')->where(array('cid'=>$goods['cid']))->find();
		$this->assign('category',$category);
		$this->assign("goods",$goods);
		//向库存当中分配选择不同库存的价格
		//向页面分配栏目的信息
		$this->assign("categoryInfo",category());
		//分配热门搜索
		$this->assign("hot",hot());
		$this->assign('stock',$stock);
		$this->assign("pic",$pic);
		$this->assign("stockData",$stockData);
		$this->assign("goods_attr",$goods_attr);
		$this->assign("attrClass",$attrClass);
		//分配购物车里面的商品
		$this->assign("cart",cart());
		$this->display("content");
	}


	// 判断是否有库存
	public function getStock(){
		// p(Q(""));
		$attr=Q("attr");
		$gid=Q("gid");
		$attr=explode("-",$attr);
		if($this->getStockId($gid,$attr)){
			echo 1;exit;
		}else{
			echo 0;exit;
		}

	}
	//获得库存的id
	function getStockId($gid,$attr){
		// p($gid);
		// p($attr);
		// select * from stock_attr as a1 join stock_attr as a2 
		//  on a1.stock_st_id=a2.stock_st_id 
		// where a1.attr_value_av_id=$attr and a2.attr_value_av_id=$attr and gid=$gid
		//组合sql语句准确的来查找库存
		$table=C("DB_PREFIX")."stock_attr";
		$JOIN='';
		foreach($attr as $n=>$avid){
			$as = " as a".$n;
			$JOIN.=$table.$as." join ";
		}
		$JOIN=substr($JOIN,0,-5);
		// p($JOIN);
		$ON = " on ";
		for($i=0;$i<count($attr)-1;$i++){
			$a="a".$i;
			// on a1.stock_st_id=a2.stock_st_id 
			$na="a".($i+1);
			$ON.=$a.".stock_st_id=".$na.".stock_st_id and ";
		}
		// $ON=substr($ON,0,-4);
		$ON=$ON." st.st_id=a0.stock_st_id ";
		// p($ON);
		$WHERE="";
		foreach($attr as $n=>$avid){
			$a="a".$n;
			$WHERE.=$a.".attr_value_av_id=".$avid." and ";
		}
		$WHERE=" where ".$WHERE."st.goods_gid=".$gid;
		$sql="select * from stock as st join ".$JOIN.$ON.$WHERE;
		// p($sql);
		$WHERE=substr($WHERE,0,-4);
		$result=M()->query($sql);
		// var_dump($result);
		return $result=$result?$result:null;
	}



	//想栏目分配商品分配商品内容
	public function assignGoods($cid){
		$s=Q("s");
		$s=explode("-",$s);
		$searchGoods=array();
		foreach($s as $avid){
			if($avid==0)continue;//去掉为0的属性值
			$searchGoods[]=$avid;
			}
			// p($searchGoods);
			//如果为空的话,把该栏目的内容全部给弄出来
			if(empty($searchGoods)){
				$count=M("goods")->where(array("cid"=>$cid))->count();
				 // p($count);
				$page=new page($count,3);
				$goods=M("goods")->where(array('cid'=>$cid))->limit($page->limit())->all();
			}else{
				//根据商品属性来筛选数据
				//组合sql语句用来检索商品
				//select * from   goods_attr as a1 join goods_attr as a2 join goods_attr as a3 
				//on a1.gid=a2.gid and a2.gid=a3.gid
				//where avid=2 and avid=3 and avid=5;
				$table=C("DB_PREFIX")."goods_attr";
				$JOIN="";
				foreach($searchGoods as $n=>$a){
					$as=' as a'.$n.' join ';
					$JOIN.=$table.$as;
				}
				$JOIN=substr($JOIN,0,-5);
				// p($JOIN);
				$ON="on ";
				for($i=0;$i<count($searchGoods)-1;$i++){
					$a1="a".$i;
					$a2="a".($i+1);
					$ON.=$a1.".gid = ".$a2.".gid and ";
				}
				// $ON=substr($ON,0,-4);
				$ON=$ON."g.gid=a0.gid";
				// p($ON);
				$WHERE=" where ";
				foreach($searchGoods as $n=>$avid){
					$as="a".$n;
					$WHERE.=$as.".avid=".$avid." and ";
				}
				$WHERE=substr($WHERE,0,-4);
				//统计商品的数量
				$Countsql="select count(*)  as c from goods as g join ".$JOIN.$ON.$WHERE." and g.cid=".Q("cid");
				// 生成分页，让每一页显示数量一样多的商品
				$countSql=M()->query($Countsql);
				// p($countSql);die;
				$page=new page($countSql[0]['c'],6);
				//获得该栏目下面对应商品属性的所有的商品
				$sql="select * from goods as g join ".$JOIN.$ON.$WHERE." and g.cid=".Q("cid")." limit ".$page->limit(true);//如果不加上true的话，数据是错误的。limit是一个数组
				$goods=M()->query($sql);
				// p($goods);
	               }
	               //向模版分配商品
	               // p($goods);
	               $this->assign("page",$page->show());
	               $this->assign("goods",$goods);

	}
	//分配商品搜索属性
	public function assignSearchAttr($gtid){
		$attrClass=M("g_attr_class")->where(array("add_goods_type_aid"=>$gtid))->all();
		// p($attrClass);
		$Model=M("g_arr_value");
		foreach($attrClass as $n=>$attr){
			$attrValue=$Model->where(array("g_attr_class_ac_id"=>$attr['ac_id']))->all();
			$attrClass[$n]['attr']=$attrValue;
		}
		// p($attrClass);
		$this->assign('attrClass',$attrClass);
	}
	//修正一下display默认引用模版的位置
	public function display($tpl=null,$cacheTime = null, $cachePath = null, $stat = false, $contentType = "text/html", $charset = "", $show = true){    
		$tpl=C("TPL_PATH")."/".C("TPL_STYLE")."/".$tpl;
		parent::display($tpl);
	}
}
